package com.pxst.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pxst.agent.entity.SysAgents;
import com.pxst.agent.mapper.SysAgentsMapper;
import com.pxst.merchant.entity.SysMerchantAdjust;
import com.pxst.merchant.entity.SysMerchantInfo;
import com.pxst.merchant.mapper.SysMerchantAdjustMapper;
import com.pxst.merchant.req.MonthlyCreatReq;
import com.pxst.merchant.req.MonthlyReq;
import com.pxst.merchant.resp.MonthlyResp;
import com.pxst.merchant.service.SysMerchantAdjustService;
import com.pxst.resp.ApiResp;
import icu.mhb.mybatisplus.plugln.base.service.impl.JoinServiceImpl;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author zarke
* @since 2023-09-14 09:56:34
*/
@Service
@Transactional
@Slf4j
public class SysMerchantAdjustServiceImpl extends JoinServiceImpl<SysMerchantAdjustMapper, SysMerchantAdjust> implements SysMerchantAdjustService {

    @Autowired
    private SysMerchantAdjustMapper sysMerchantAdjustMapper;

    @Autowired
    private SysMerchantAdjustService sysMerchantAdjustService;

    @Autowired
    private SysAgentsMapper sysAgentsMapper;

    /**
     * 月调整查询
     * @param monthlyReq
     * @return
     */
    @Override
    public ApiResp<MonthlyResp> getAdjust(MonthlyReq monthlyReq) {
        //用月调整表的商户id查询商户名称，在用商户信息表中的代理id去查询代理名称
        JoinLambdaWrapper<SysMerchantAdjust> jlw = new JoinLambdaWrapper<>(SysMerchantAdjust.class);
        //定义SysMerchantAdjust关联的SysMerchantInfo对象
        jlw.leftJoin(SysMerchantInfo.class,SysMerchantInfo::getId,SysMerchantAdjust::getMerchantId)
                //定义SysMerchantAdjust的查询条件
                .apply(StringUtils.hasText(monthlyReq.getCurrency()),"sys_merchant_adjust.currency = {0}",monthlyReq.getCurrency())
                .apply(monthlyReq.getMerchantId() != null,"merchant_id = {0}",monthlyReq.getMerchantId())
                .apply(monthlyReq.getMonth() != null,"month = {0}",monthlyReq.getMonth())
                .apply(monthlyReq.getYear() != null,"year = {0}",monthlyReq.getYear())
                //定义SysMerchantInfo要返回的字段
                .selectAs((cb) -> {
                    cb.add(SysMerchantInfo::getAgentId)
                        .addFunAlias(SysMerchantInfo::getName,MonthlyResp::getMerchantName);
                })
                .end();
        //定义SysMerchantInfo关联的SysAgents对象
        jlw.leftJoin(SysAgents.class,SysAgents::getId,SysMerchantInfo::getAgentId)
                .selectAs(SysAgents::getFullName,MonthlyResp::getAgentName)
                .end();

        PageHelper.startPage(monthlyReq.getPage(),monthlyReq.getRp());
        Page<MonthlyResp> page = (Page<MonthlyResp>) sysMerchantAdjustService.joinList(jlw, MonthlyResp.class);

        return ApiResp.page(page);
    }

    /**
     * 新增调整
     * @param monthlyCreatReq
     * @return
     */
    @Override
    public ApiResp<SysMerchantAdjust> create(MonthlyCreatReq monthlyCreatReq) {
        //先将月调整的基础信息保存，再将代理表的商户数量加一
        SysMerchantAdjust sysMerchantAdjust = new SysMerchantAdjust();
        BeanUtils.copyProperties(monthlyCreatReq,sysMerchantAdjust);
        sysMerchantAdjustMapper.insert(sysMerchantAdjust);
        SysAgents sysAgents = sysAgentsMapper.selectById(monthlyCreatReq.getAgentId());
        sysAgents.setMerchantCount(sysAgents.getMerchantCount() + 1);
        sysAgentsMapper.updateById(sysAgents);
        return ApiResp.sucess(sysMerchantAdjustMapper.selectById(sysMerchantAdjust.getAdjustId()));
    }
}